const Controller = require("egg").Controller;

let cla = '';

class siptime {
    // constructor(time){
    //     this.time = time;
    // }

    test(time) {
        let rg = /Epoch Time: (\S*) seconds/;//第五行
        let rg1 = /Source Address: (\S*)/;//地三十九行
        let rg2 = /Destination Address: (\S*)/;//第四十行
        let rg3 = /Source Port: (\S*)/;//第四十二行
        let rg4 = /Destination Port: (\S*)/;//第四十三行
        let rg5 = /Session Initiation Protocol \S(\S*)\S/;//第五十二行
        let rg6 = /SIP to address Host Part: (\S*)/;//第七十八行
        let rg7 = /SIP from address Host Part: (\S*)/;//第八十三行
        let rg8 = /SIP from tag: (\S*)/;
        let rg9 = /Generated Call-ID: (\S*).]/;
        if (rg.test(time)) {
            // time = time.substring(12, 32);
            time = time.match(rg)[1];
            return time;
        }
        else if (rg1.test(time)) {
            time = time.match(rg1)[1];
            return time;
        }
        else if (rg2.test(time)) {
            time = time.match(rg2)[1];
            return time;
        }
        else if (rg3.test(time)) {
            time = time.match(rg3)[1];
            return time;
        }
        else if (rg4.test(time)) {
            time = time.match(rg4)[1];
            return time;
        }
        else if (rg5.test(time)) {
            time = time.match(rg5)[1];
            return time;
        }
        else if (rg6.test(time)) {
            time = time.match(rg6)[1];
            return time;
        }
        else if (rg7.test(time)) {
            time = time.match(rg7)[1];
            return time;
        }
        else if (rg8.test(time)) {
            time = time.match(rg8)[1];
            return time;
        }
        else if (rg9.test(time)) {
            time = time.match(rg9)[1];
            return time;
        }
        else {
            return 0;
        }

    }
}

class ClazzController extends Controller {

    //restful: index/create/destroy/update
    // 不做任何异常处理



    async index() {
        let id = this.ctx.request.query.id;
        let clazzList = await this.app.model.Clazz.findAll();
        // let clazzList = cla;

        // let name1 = [];
        // let name2 = [];
        // let siptimes = new siptime();

        // clazzList.forEach((item) => {
        //     if (item.trim() != '') {
        //         item = siptimes.test(item);
        //         if(item){
        //             name1.push(item);
        //         }
        //     }
        //     else {
        //         name2.push(name1);
        //         name1 = [];
        //     }
        // })
        // let sort1 = [];
        // while (clazzList.length != 0) {
        //     let arr = [];
        //     let flag = clazzList[0];
        //     arr.push(clazzList[0]);
        //     clazzList.splice(0, 1);
        //     for (let i = 0; i < clazzList.length; i++) {
        //         if (flag.callid.trim() == clazzList[i].callid.trim()) {
        //             arr.push(clazzList[i]);
        //             clazzList.splice(i, 1);
        //         }
        //     }
        //     sort1.push(arr);
        // }


        let dataInfo = {}
        clazzList.forEach((item, index) => {
            let { callid } = item
            if (!dataInfo[callid]) {
                dataInfo[callid] = {
                    callid,
                    children: [],
                }
            }
            dataInfo[callid].children.push(item);
        })
        let list = Object.values(dataInfo)


        this.ctx.body = list;
    }

    async create() {
        let name = this.ctx.request.body.name;

        // 尝试以下
        let name1 = [];
        let name2 = [];
        let siptimes = new siptime();

        name.forEach((item) => {
            if (item.trim() != '') {
                item = siptimes.test(item);
                if (item) {
                    name1.push(item);
                }
            }
            else {
                name2.push(name1);
                name1 = [];
            }
        })
        //尝试一下
        let valueArr = [];
        for (let i = 0; i <= name2.length - 1; i++) {
            var value = {
                time: name2[i][0],
                srcip: name2[i][1],
                dstip: name2[i][2],
                siptype: name2[i][5],
                callid: name2[i][9],
            };
            valueArr.push(value);
        }

        // cla = name2;
        cla = valueArr;
        // await this.app.model.Clazz.bulkcreate(valueArr)

        // await this.app.model.Clazz.create({
        //     name: cla
        // })
        // this.ctx.body = "添加成功"
        // let text = this.ctx.request.body.text;
        // this.ctx.body = text;




        // //测试一下，调试代码
        // let arr = [{
        //     time: '20'
        // }, {
        //     time: '20'
        // }, {
        //     time: '20'
        // }];
        let res = await this.app.model.Clazz.bulkCreate(valueArr);
        // 成功
        this.ctx.body = res;


    }

    async destroy() {
        let id = this.ctx.params.id;
        await this.app.model.Clazz.destroy({
            where: {
                id: id
            }
        })
        this.ctx.body = "删除班级数据"
    }

    async update() {
        let id = this.ctx.params.id;
        let name = this.ctx.request.body.name;
        await this.app.model.Clazz.update({ name: name }, {
            where: {
                id: id
            }
        })
        this.ctx.body = "修改成功"
    }

}



module.exports = ClazzController;